package P036_UseConstructionCode;

import org.junit.Test;

/**
 * @ClassName : UseConstructionCode
 * @Description : 使用构造代码精炼程序
 * @Author : wukj1
 * @Date: 2022/3/8 14:48
 **/
public class UseConstructionCode {

    //Java中一共四种代码块： 1、普通代码块  2、静态代码块  3、同步代码块  4、构造代码块

    @Test
    public void test(){
        Client client = new Client();
//        Client client1 = new Client("client");
    }

    class Client{
        {
            System.out.println("执行构造代码块");
        }
//        public Client(){
//            System.out.println("执行构造代码块");
//            System.out.println("执行无参构造");
//        }
//        public Client(String _str){
//            System.out.println("执行构造代码块");
//            System.out.println("执行有参构造");
//        }
    }

}

//用处一：初始化实例变量
class Worker{
    private String factoryName;
    private String department;
    private int age;
    private String name;
    {
        factoryName = "factory-01";
        department = "department-02";
    }

    public Worker(){
        this.age = 1;
        this.name = "worker-01";
    }

    public Worker(int age,String name){
        this.age = age;
        this.name = name;
    }

}

//用处二：初始化实例环境，比如说：在JEE中，产生Http Request必须首先建立Http Session
